package com.zhenyi.wumian.index;

import com.cenjiawen.common.IndexController;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import com.zhenyi.websocket.core.wumain.ChannelGroups;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class WMController extends IndexController {

	@Override
	public void submit() {
		String content = getPara("content");
		if (StrKit.isBlank(content)) {
			renderSuccess();
			return;
		}
		Ret ret = Ret.by("status", 200);
		ret.set("size", ChannelGroups.size());
		ret.set("content", content);
		ret.set("type", 1);
		ChannelGroups.broadcast(new TextWebSocketFrame(ret.toJson()));
		renderSuccess();
	}

	@Before(CacheInterceptor.class)
	@CacheName("1min")
	@Override
	public void info() {
		int hour = DateUtil.thisHour(true);
		int ran = 100;
		if (hour >= 23 || hour <= 6) {
			ran = ran + RandomUtil.randomInt(99, 300);
		} else {
			ran = ran + RandomUtil.randomInt(23, 50);
		}
		int size = ChannelGroups.size() + ran;
		renderSuccess(size);
	}

	public void bg() {
		String[] bgs = { "https://i.loli.net/2018/12/23/5c1f87606c1b7.jpg",
				"https://i.loli.net/2018/12/23/5c1f876076004.jpg", "https://i.loli.net/2018/12/23/5c1f87607da10.jpg",
				"https://i.loli.net/2018/12/23/5c1f8760a3a6b.jpg" };
		int idx = RandomUtil.randomInt(0, 3);
		renderSuccess(bgs[idx]);
	}
}
